import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:niuniu/common/constant/AppColor.dart';
import 'package:niuniu/common/constant/AppSpace.dart';
import 'package:niuniu/pages/home/controller.dart';
import 'package:get/get.dart';

class HomePage extends GetView<HomeController> {
  const HomePage({Key? key}) : super(key: key);

  // 导航栏
  AppBar buildAppBar() {
    return AppBar(
      // 背景透明
      backgroundColor: Colors.transparent,
      // 取消阴影
      elevation: 0,
      // 标题栏左侧间距
      titleSpacing: AppSpace.listItem,
      // 右侧的按钮区
      actions: [],
    );
  }

  // 主视图
  Widget buildView() {
    return SafeArea(
        child: Container(
      margin: EdgeInsets.only(left: 12.w, right: 12.w),
      padding: EdgeInsets.only(left: 12.w, right: 12.w),
      child: ListView(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(
                width: 60.w,
                height: 60.w,
                child: TextField(
                  textAlign: TextAlign.center,
                  style:
                      TextStyle(fontSize: 25.sp, fontWeight: FontWeight.w500),
                  controller: controller.oneTextController,
                  focusNode: controller.oneFocusNode,
                  cursorColor: AppColor.themeColor,
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.zero,
                    border: OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.circular(6.r),
                    ),
                  ),
                  onChanged: (val) {},
                  onSubmitted: (val) {},
                ),
              ),
              FaIcon(
                FontAwesomeIcons.plus,
                color: Colors.black,
                size: 30.sp,
              ),
              SizedBox(
                width: 60.w,
                height: 60.w,
                child: TextField(
                  keyboardType: TextInputType.number,
                  textAlign: TextAlign.center,
                  style:
                      TextStyle(fontSize: 25.sp, fontWeight: FontWeight.w500),
                  controller: controller.twoTextController,
                  focusNode: controller.twoFocusNode,
                  cursorColor: AppColor.themeColor,
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.zero,
                    border: OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.circular(6.r),
                    ),
                  ),
                  onChanged: (val) {},
                  onSubmitted: (val) {},
                ),
              ),
              FaIcon(
                FontAwesomeIcons.plus,
                color: Colors.black,
                size: 30.sp,
              ),
              SizedBox(
                width: 60.w,
                height: 60.w,
                child: TextField(
                  textAlign: TextAlign.center,
                  style:
                      TextStyle(fontSize: 25.sp, fontWeight: FontWeight.w500),
                  controller: controller.threeTextController,
                  focusNode: controller.threeFocusNode,
                  cursorColor: AppColor.themeColor,
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding: EdgeInsets.zero,
                    border: OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.circular(6.r),
                    ),
                  ),
                  onChanged: (val) {},
                  onSubmitted: (val) {},
                ),
              ),
            ],
          ),
          SizedBox(height: 30.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              FaIcon(
                FontAwesomeIcons.question,
                color: Colors.red,
                size: 30.sp,
              ),
              FaIcon(
                FontAwesomeIcons.plus,
                color: Colors.black,
                size: 30.sp,
              ),
              FaIcon(
                FontAwesomeIcons.question,
                color: Colors.red,
                size: 30.sp,
              ),
            ],
          ),
          SizedBox(height: 30.h),
          Align(
            alignment: Alignment.center,
            child: FaIcon(
              FontAwesomeIcons.anglesDown,
              color: Colors.black,
              size: 30.sp,
            ),
          ),
          SizedBox(height: 40.h),
          Align(
            alignment: Alignment.center,
            child: SizedBox(
              width: 60.w,
              height: 60.w,
              child: TextField(
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 25.sp, fontWeight: FontWeight.w500),
                controller: controller.resultTextController,
                focusNode: controller.resultFocusNode,
                cursorColor: AppColor.themeColor,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Colors.white,
                  contentPadding: EdgeInsets.zero,
                  border: OutlineInputBorder(
                    borderSide: BorderSide.none,
                    borderRadius: BorderRadius.circular(6.r),
                  ),
                ),
                onChanged: (val) {},
                onSubmitted: (val) {},
              ),
            ),
          ),
          controller.isShowResult
              ? SizedBox(height: 30.h)
              : const SizedBox(height: 0),
          controller.isShowResult
              ? Align(
                  alignment: Alignment.center,
                  child: GestureDetector(
                      child: Text(
                        "可能的答案组合：${controller.correctDtaList.length} 个",
                        style: TextStyle(
                          fontSize: 18.sp,
                          color: Colors.green,
                        ),
                      ),
                      onTap: () {
                        Get.bottomSheet(bottomAnswerList("correct"));
                      }),
                )
              : Container(),
          controller.isShowResult
              ? SizedBox(height: 15.h)
              : const SizedBox(height: 0),
          controller.isShowResult
              ? Align(
                  alignment: Alignment.center,
                  child: GestureDetector(
                      child: Text(
                        "不可能的答案组合：${controller.errorDtaList.length} 个",
                        style: TextStyle(
                          fontSize: 18.sp,
                          color: Colors.red,
                        ),
                      ),
                      onTap: () {
                        Get.bottomSheet(bottomAnswerList("error"));
                      }),
                )
              : Container(),
          SizedBox(height: 150.h),
          MaterialButton(
              color: Colors.cyanAccent,
              child: Text(
                "鉴定",
                style: TextStyle(fontSize: 30.sp),
              ),
              onPressed: () {
                if ("" == controller.oneTextController.text) {
                  EasyLoading.showToast('请为第一个框输入数字');
                  return;
                }
                if (controller.oneTextController.text.length > 1) {
                  EasyLoading.showToast('请在第一个框输入 0 ～ 9 数字');
                  return;
                }
                if ("" == controller.twoTextController.text) {
                  EasyLoading.showToast('请为第二个框输入数字');
                  return;
                }
                if (controller.twoTextController.text.length > 1) {
                  EasyLoading.showToast('请在第二个框输入 0 ～ 9 数字');
                  return;
                }
                if ("" == controller.threeTextController.text) {
                  EasyLoading.showToast('请为第三个框输入数字');
                  return;
                }
                if (controller.threeTextController.text.length > 1) {
                  EasyLoading.showToast('请在第三个框输入 0 ～ 9 数字');
                  return;
                }
                if ("" == controller.resultTextController.text) {
                  EasyLoading.showToast('请为第四个框输入数字');
                  return;
                }
                if (controller.resultTextController.text.length > 1) {
                  EasyLoading.showToast('请在第四个框输入 0 ～ 9 数字');
                  return;
                }
                controller.oneFocusNode.unfocus();
                controller.twoFocusNode.unfocus();
                controller.threeFocusNode.unfocus();
                controller.resultFocusNode.unfocus();
                controller.correctDtaList.clear();
                controller.errorDtaList.clear();
                controller.identify();
                controller.isShowResult = true;
                controller.update(["home-page"]);
              }),
          SizedBox(height: 30.h),
          MaterialButton(
              color: Colors.cyanAccent,
              child: Text(
                "刷新",
                style: TextStyle(fontSize: 30.sp),
              ),
              onPressed: () {
                controller.oneTextController.text = "";
                controller.twoTextController.text = "";
                controller.threeTextController.text = "";
                controller.resultTextController.text = "";
                controller.oneFocusNode.unfocus();
                controller.twoFocusNode.unfocus();
                controller.threeFocusNode.unfocus();
                controller.resultFocusNode.unfocus();
                controller.isShowResult = false;
                controller.update(["home-page"]);
              }),
        ],
      ),
    ));
  }

  bottomAnswerList(String type) {
    return Container(
      padding: EdgeInsets.only(top: 12.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.r),
        color: Colors.white,
      ),
      child: Column(
        children: [
          type == "correct"
              ? Text(
                  "可能的答案组合",
                  style: TextStyle(
                    fontSize: 22.h,
                    color: Colors.green,
                  ),
                )
              : Text(
                  "不可能的答案组合",
                  style: TextStyle(
                    fontSize: 22.h,
                    color: Colors.red,
                  ),
                ),
          SizedBox(height: 24.h),
          type == "correct"
              ? Expanded(
                  child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) => Container(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(left: 12.w, right: 12.w),
                        padding: EdgeInsets.only(
                          left: 12.w,
                          right: 12.w,
                          top: 12.w,
                          bottom: 12.h,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text(
                              controller.correctDtaList[index][0].toString(),
                              style: TextStyle(fontSize: 20.sp),
                            ),
                            SizedBox(width: 60.w),
                            Text(
                              controller.correctDtaList[index][1].toString(),
                              style: TextStyle(fontSize: 20.sp),
                            ),
                          ],
                        )),
                    separatorBuilder: (BuildContext context, int index) {
                      return const Divider(height: 0);
                    },
                    itemCount: controller.correctDtaList.length,
                  ),
                )
              : Expanded(
                  child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) => Container(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(left: 12.w, right: 12.w),
                        padding: EdgeInsets.only(
                          left: 12.w,
                          right: 12.w,
                          top: 12.w,
                          bottom: 12.h,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text(
                              controller.errorDtaList[index][0].toString(),
                              style: TextStyle(fontSize: 20.sp),
                            ),
                            SizedBox(width: 60.w),
                            Text(
                              controller.errorDtaList[index][1].toString(),
                              style: TextStyle(fontSize: 20.sp),
                            ),
                          ],
                        )),
                    separatorBuilder: (BuildContext context, int index) {
                      return const Divider(height: 0);
                    },
                    itemCount: controller.errorDtaList.length,
                  ),
                ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(
      // init: HomeController(),
      init: Get.find<HomeController>(),
      id: "home-page",
      builder: (_) {
        return Scaffold(
          appBar: buildAppBar(),
          body: buildView(),
        );
      },
    );
  }
}
